<?php

namespace app\erp\model;

use think\Model;

class Baojiadb extends Model
{
    protected $pk = 'baojia_id';
    protected $name = 'baojia';
    public $param = array();
    protected $type = [
        'create_time' => 'timestamp:H:i d-m-Y',
        'audit_time' => 'timestamp:H:i d-m-Y',
        'zhouqi' => 'timestamp:Y-m-d'
    ];
    
    protected $append = ['active'];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    // 验证当前数据是否是有效期内
    public function getActiveAttr($value, $row)
    {
        $get_new_data = $this
            ->where('goods_sku', $row['goods_sku'])
            ->whereTime('zhouqi', '>', time())
            ->where('apply_member_id', $row['apply_member_id'])
            ->count();
        return $get_new_data ? true : false;
    }

    public function users()
    {
        return $this->belongsTo('Memberdb', 'apply_member_id');
    }

    public function admins()
    {
        return $this->belongsTo('Memberdb', 'audit_member_id');
    }

    public function getLilvAttr($value)
    {
        return number_format(abs($value) * 100, 2, '.', '');
    }

    protected function base($query)
    {
        if (isset($this->param['apply_member_id']) && $this->param['apply_member_id']) {
            $query->where('apply_member_id', $this->param['apply_member_id']);
        }
        if (isset($this->param['goods_type']) && $this->param['goods_type']) {
            $query->where('goods_type', $this->param['goods_type']);
        }
        if (isset($this->param['goods_sku']) && $this->param['goods_sku']) {
            $query->whereLike('goods_sku', '%' . $this->param['goods_sku'] . '%');
        }
        if (isset($this->param['goods_shuzi']) && $this->param['goods_shuzi']) {
            $query->whereLike('goods_shuzi', '%' . $this->param['goods_shuzi'] . '%');
        }
        if (isset($this->param['beizhu']) && $this->param['beizhu']) {
            $query->whereLike('beizhu', '%' . $this->param['beizhu'] . '%');
        }
        if (isset($this->param['goods_name']) && $this->param['goods_name']) {
            $query->whereLike('goods_name', '%' . $this->param['goods_name'] . '%');
        }
        if (isset($this->param['apply_member']) && $this->param['apply_member']) {
            $query->whereIn("apply_member_id", Memberdb::whereLike('supplier_name', '%' . $this->param['apply_member'] . '%')->column('member_id'));
        } 
        if (isset($this->param['audit_member']) && $this->param['audit_member']) {
            $query->whereIn("audit_member_id", Memberdb::whereLike('realname', '%' . $this->param['audit_member'] . '%')->column('member_id'));
        }
        if (isset($this->param['min_create_time']) && $this->param['min_create_time']) {
            $query->whereTime('create_time', '>=', $this->param['min_create_time']);
        }
        if (isset($this->param['max_create_time']) && $this->param['max_create_time']) {
            $query->whereTime('create_time', '<=', $this->param['max_create_time'] . ' 23:59:59');
        }
        if (isset($this->param['min_audit_time']) && $this->param['min_audit_time']) {
            $query->whereTime('audit_time', '>=', $this->param['min_audit_time']);
        }
        if (isset($this->param['max_audit_time']) && $this->param['max_audit_time']) {
            $query->whereTime('audit_time', '<=', $this->param['max_audit_time'] . ' 23:59:59');
        }
        if (isset($this->param['min_zhouqi_time']) && $this->param['min_zhouqi_time']) {
            $query->whereTime('zhouqi', '>=', $this->param['min_zhouqi_time']);
        }
        if (isset($this->param['max_zhouqi_time']) && $this->param['max_zhouqi_time']) {
            $query->whereTime('zhouqi', '<=', $this->param['max_zhouqi_time'] . ' 23:59:59');
        }
        if (isset($this->param['audit_state']) && $this->param['audit_state']) {
            $query->where('audit_state', $this->param['audit_state']);
        }
    }
}